/**    
* @Title: CheckUtil.java  
* @Package com.kaver.capital.service.utils   
* @author roc   
* @date 2019年8月20日 下午5:47:41  
* @version V1.0  
* @copyright kaver 
*/
package com.kaver.sso.utils;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import com.kaver.sso.def.BizException;
import com.kaver.sso.def.enums.CodeEnum;


/**  
* @ClassName: CheckUtil  
* @Description: 参数校验
* @author roc  
* @date 2019年8月20日 下午5:47:41  
*    
*/
public class CheckUtil {
    private static Validator validator;

    static {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        validator = factory.getValidator();
    }

    public static void checkRequest(Object request) throws BizException {
        if (request == null) {
            throw new BizException(CodeEnum.F_000001.getCode(), "参数不能为空");
        }
        Set<ConstraintViolation<Object>> constraintViolations = validator.validate(request);
        if (constraintViolations != null && !constraintViolations.isEmpty()) {
            for (ConstraintViolation<Object> constraintViolation : constraintViolations) {
                throw new BizException(CodeEnum.F_000001.getCode(), "[" + constraintViolation.getPropertyPath() + "]" + constraintViolation.getMessage());
            }
        }
    }

    public static String check(Object body) {
        try {
            CheckUtil.checkRequest(body);
            return null;
        } catch (BizException e) {
            return e.getMessage();
        }
    }

}
